/************************************************************************************
* FileSystemCollectionLoader.cpp                                                    *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "FileSystemCollectionLoader.h"
#include "common/Constants.h"
#include "common/Utils.h"
#include "database/YDb.h"

#include <QDir>
#include <QList>
#include <QMessageBox>
#include <QUuid>
#include <audioproperties.h>
#include <fileref.h>
#include <tag.h>

using namespace TagLib;
using namespace Phonon;

FileSystemCollectionLoader::FileSystemCollectionLoader(QObject* parent)
    :FileSystemMediaLoader(parent){
    m_colItems = new QList<CollectorItem*>();
}

FileSystemCollectionLoader::~FileSystemCollectionLoader() {
    clear();
    delete m_colItems;
}


void FileSystemCollectionLoader::run() {
    YDb::get()->updater()->clearRecentAddtions();
    FileSystemMediaLoader::run();
//    YDb::get()->updater()->addRecentAdditions(*m_colItems);
    emit finishedLoading(m_colItems);
}


void FileSystemCollectionLoader::clear() {
    FileSystemMediaLoader::clear();
    m_colItems->clear();
}


void FileSystemCollectionLoader::addToMediaList(MediaSource *source) {
    QString uuid = QUuid().createUuid().toString();
    CollectorItem* item = new CollectorItem(uuid, source);
    FileRef ref = FileRef(source->fileName().toUtf8());
    item->setMetaData(TStringToQString(ref.tag()->title()),
                      TStringToQString(ref.tag()->artist()),
                      TStringToQString(ref.tag()->album()),
                      QString("%1").arg(ref.tag()->year()),
                      TStringToQString(ref.tag()->genre()),
                      TStringToQString(ref.tag()->comment()),
                      ref.tag()->track(),
                      ref.audioProperties()->length(),
                      ref.audioProperties()->bitrate(),
                      ref.audioProperties()->sampleRate(),
                      ref.audioProperties()->channels());
//    item->setUuid();
    YDb::get()->updater()->addCollectionItem(item);
    YDb::get()->updater()->addRecentAddition(item);
    m_colItems->append(item);
    emit loaded(item);
}
